package com.ygqh.customs.handler;

import com.ygqh.common.utils.StringUtil;
import com.ygqh.customs.config.CustomsConfig;
import com.ygqh.customs.domain.PayExchangeInfoHead;
import com.ygqh.customs.model.OrderData;
import com.ygqh.customs.po.YgOrderPayLog;
import com.ygqh.customs.utils.XMLParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/3/18 17:59
 * @since 1.0.0
 */
@Slf4j
public class PayExchangeInfoHeadHandler {

    private CustomsConfig customsConfig;
    private OrderData orderData;

    private String payRequest;

    private String payNotify;

    private Map<String, Object> payNotifyMap;

    public PayExchangeInfoHeadHandler(OrderData orderData, List<YgOrderPayLog> payLogList, CustomsConfig customsConfig) {
        this.customsConfig = customsConfig;
        this.orderData = orderData;
        init(payLogList);
    }

    private void init(List<YgOrderPayLog> payLogList) {
        if (CollectionUtils.isEmpty(payLogList)) {
            log.error("buildPayExchangeInfoHead fail:找不到支付流水号：{} 的支付记录", orderData.getPaymentCode());
        }
        Map<String, List<YgOrderPayLog>> collect = payLogList.stream().collect(Collectors.groupingBy(YgOrderPayLog::getLogType));
        List<YgOrderPayLog> requestList = collect.get("Resquest");
        if (CollectionUtils.isEmpty(payLogList)) {
            log.error("buildPayExchangeInfoHead fail:找不到支付流水号：{} 的统一下单请求记录", orderData.getPaymentCode());
        } else {
            payRequest = StringUtil.replaceBlank(requestList.get(0).getContent());
        }
        List<YgOrderPayLog> notifyList = collect.get("Notify");
        if (CollectionUtils.isEmpty(notifyList)) {
            log.error("buildPayExchangeInfoHead fail:找不到支付流水号：{} 的异步通知记录", orderData.getPaymentCode());
        } else {
            payNotify = StringUtil.replaceBlank(notifyList.get(0).getContent());
            try {
                payNotifyMap = XMLParser.getMapFromXML(payNotify);
            } catch (ParserConfigurationException | SAXException | IOException e) {
                e.printStackTrace();
            }
        }
    }


    private String getGuid() {
        return UUID.randomUUID().toString().toUpperCase();
    }

    private BigDecimal getTotalAmount() {
        return new BigDecimal(payNotifyMap.get("total_fee").toString()).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_UNNECESSARY);
    }

    private String getTradingTime() {
        return payNotifyMap.get("time_end").toString();
    }

    public PayExchangeInfoHead buildPayExchangeInfoHead() {
        PayExchangeInfoHead infoHead = new PayExchangeInfoHead();
        infoHead.setGuid(getGuid());
        infoHead.setEbpCode(customsConfig.getEbpCode());
        infoHead.setPayCode(customsConfig.getPayCodeWechat());
        infoHead.setPayTransactionId(payNotifyMap.get("transaction_id").toString());
        infoHead.setTotalAmount(getTotalAmount());
        infoHead.setCurrency(customsConfig.getCurrency());
        infoHead.setVerDept(customsConfig.getVerDept());
        infoHead.setPayType(customsConfig.getPayType());
        infoHead.setTradingTime(getTradingTime());
        infoHead.setInitalRequest(payRequest);
        infoHead.setInitalResponse(payNotify);
        return infoHead;
    }


}
